/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package logica.jpaControllers;

import Entidades.Transportista;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Entidades.Tipodocumento;
import Entidades.Provincia;
import Entidades.Pais;
import Entidades.Localidad;
import Entidades.Transportetransportista;
import java.util.ArrayList;
import java.util.Collection;
import Entidades.Recepcion;
import logica.jpaControllers.exceptions.IllegalOrphanException;
import logica.jpaControllers.exceptions.NonexistentEntityException;

/**
 *
 * @author Anali
 */
public class TransportistaJpaController {

    public TransportistaJpaController() {
        emf = Persistence.createEntityManagerFactory("GestorPesajeJpaPU2");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Transportista transportista) {
        if (transportista.getTransportetransportistaCollection() == null) {
            transportista.setTransportetransportistaCollection(new ArrayList<Transportetransportista>());
        }
        if (transportista.getRecepcionCollection() == null) {
            transportista.setRecepcionCollection(new ArrayList<Recepcion>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Tipodocumento tipodocumento = transportista.getTipodocumento();
            if (tipodocumento != null) {
                tipodocumento = em.getReference(tipodocumento.getClass(), tipodocumento.getIdtipodocumento());
                transportista.setTipodocumento(tipodocumento);
            }
            Provincia provincia = transportista.getProvincia();
            if (provincia != null) {
                provincia = em.getReference(provincia.getClass(), provincia.getIdprovincia());
                transportista.setProvincia(provincia);
            }
            Pais pais = transportista.getPais();
            if (pais != null) {
                pais = em.getReference(pais.getClass(), pais.getIdpais());
                transportista.setPais(pais);
            }
            Localidad localidad = transportista.getLocalidad();
            if (localidad != null) {
                localidad = em.getReference(localidad.getClass(), localidad.getIdlocalidad());
                transportista.setLocalidad(localidad);
            }
            Collection<Transportetransportista> attachedTransportetransportistaCollection = new ArrayList<Transportetransportista>();
            for (Transportetransportista transportetransportistaCollectionTransportetransportistaToAttach : transportista.getTransportetransportistaCollection()) {
                transportetransportistaCollectionTransportetransportistaToAttach = em.getReference(transportetransportistaCollectionTransportetransportistaToAttach.getClass(), transportetransportistaCollectionTransportetransportistaToAttach.getIdtransportetransportista());
                attachedTransportetransportistaCollection.add(transportetransportistaCollectionTransportetransportistaToAttach);
            }
            transportista.setTransportetransportistaCollection(attachedTransportetransportistaCollection);
            Collection<Recepcion> attachedRecepcionCollection = new ArrayList<Recepcion>();
            for (Recepcion recepcionCollectionRecepcionToAttach : transportista.getRecepcionCollection()) {
                recepcionCollectionRecepcionToAttach = em.getReference(recepcionCollectionRecepcionToAttach.getClass(), recepcionCollectionRecepcionToAttach.getNumeroRecepcion());
                attachedRecepcionCollection.add(recepcionCollectionRecepcionToAttach);
            }
            transportista.setRecepcionCollection(attachedRecepcionCollection);
            em.persist(transportista);
            if (tipodocumento != null) {
                tipodocumento.getTransportistaCollection().add(transportista);
                tipodocumento = em.merge(tipodocumento);
            }
            if (provincia != null) {
                provincia.getTransportistaCollection().add(transportista);
                provincia = em.merge(provincia);
            }
            if (pais != null) {
                pais.getTransportistaCollection().add(transportista);
                pais = em.merge(pais);
            }
            if (localidad != null) {
                localidad.getTransportistaCollection().add(transportista);
                localidad = em.merge(localidad);
            }
            for (Transportetransportista transportetransportistaCollectionTransportetransportista : transportista.getTransportetransportistaCollection()) {
                Transportista oldTransportistaOfTransportetransportistaCollectionTransportetransportista = transportetransportistaCollectionTransportetransportista.getTransportista();
                transportetransportistaCollectionTransportetransportista.setTransportista(transportista);
                transportetransportistaCollectionTransportetransportista = em.merge(transportetransportistaCollectionTransportetransportista);
                if (oldTransportistaOfTransportetransportistaCollectionTransportetransportista != null) {
                    oldTransportistaOfTransportetransportistaCollectionTransportetransportista.getTransportetransportistaCollection().remove(transportetransportistaCollectionTransportetransportista);
                    oldTransportistaOfTransportetransportistaCollectionTransportetransportista = em.merge(oldTransportistaOfTransportetransportistaCollectionTransportetransportista);
                }
            }
            for (Recepcion recepcionCollectionRecepcion : transportista.getRecepcionCollection()) {
                Transportista oldTransportistaOfRecepcionCollectionRecepcion = recepcionCollectionRecepcion.getTransportista();
                recepcionCollectionRecepcion.setTransportista(transportista);
                recepcionCollectionRecepcion = em.merge(recepcionCollectionRecepcion);
                if (oldTransportistaOfRecepcionCollectionRecepcion != null) {
                    oldTransportistaOfRecepcionCollectionRecepcion.getRecepcionCollection().remove(recepcionCollectionRecepcion);
                    oldTransportistaOfRecepcionCollectionRecepcion = em.merge(oldTransportistaOfRecepcionCollectionRecepcion);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Transportista transportista) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Transportista persistentTransportista = em.find(Transportista.class, transportista.getIdTransportista());
            Tipodocumento tipodocumentoOld = persistentTransportista.getTipodocumento();
            Tipodocumento tipodocumentoNew = transportista.getTipodocumento();
            Provincia provinciaOld = persistentTransportista.getProvincia();
            Provincia provinciaNew = transportista.getProvincia();
            Pais paisOld = persistentTransportista.getPais();
            Pais paisNew = transportista.getPais();
            Localidad localidadOld = persistentTransportista.getLocalidad();
            Localidad localidadNew = transportista.getLocalidad();
            Collection<Transportetransportista> transportetransportistaCollectionOld = persistentTransportista.getTransportetransportistaCollection();
            Collection<Transportetransportista> transportetransportistaCollectionNew = transportista.getTransportetransportistaCollection();
            Collection<Recepcion> recepcionCollectionOld = persistentTransportista.getRecepcionCollection();
            Collection<Recepcion> recepcionCollectionNew = transportista.getRecepcionCollection();
            List<String> illegalOrphanMessages = null;
            for (Transportetransportista transportetransportistaCollectionOldTransportetransportista : transportetransportistaCollectionOld) {
                if (!transportetransportistaCollectionNew.contains(transportetransportistaCollectionOldTransportetransportista)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Transportetransportista " + transportetransportistaCollectionOldTransportetransportista + " since its transportista field is not nullable.");
                }
            }
            for (Recepcion recepcionCollectionOldRecepcion : recepcionCollectionOld) {
                if (!recepcionCollectionNew.contains(recepcionCollectionOldRecepcion)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Recepcion " + recepcionCollectionOldRecepcion + " since its transportista field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (tipodocumentoNew != null) {
                tipodocumentoNew = em.getReference(tipodocumentoNew.getClass(), tipodocumentoNew.getIdtipodocumento());
                transportista.setTipodocumento(tipodocumentoNew);
            }
            if (provinciaNew != null) {
                provinciaNew = em.getReference(provinciaNew.getClass(), provinciaNew.getIdprovincia());
                transportista.setProvincia(provinciaNew);
            }
            if (paisNew != null) {
                paisNew = em.getReference(paisNew.getClass(), paisNew.getIdpais());
                transportista.setPais(paisNew);
            }
            if (localidadNew != null) {
                localidadNew = em.getReference(localidadNew.getClass(), localidadNew.getIdlocalidad());
                transportista.setLocalidad(localidadNew);
            }
            Collection<Transportetransportista> attachedTransportetransportistaCollectionNew = new ArrayList<Transportetransportista>();
            for (Transportetransportista transportetransportistaCollectionNewTransportetransportistaToAttach : transportetransportistaCollectionNew) {
                transportetransportistaCollectionNewTransportetransportistaToAttach = em.getReference(transportetransportistaCollectionNewTransportetransportistaToAttach.getClass(), transportetransportistaCollectionNewTransportetransportistaToAttach.getIdtransportetransportista());
                attachedTransportetransportistaCollectionNew.add(transportetransportistaCollectionNewTransportetransportistaToAttach);
            }
            transportetransportistaCollectionNew = attachedTransportetransportistaCollectionNew;
            transportista.setTransportetransportistaCollection(transportetransportistaCollectionNew);
            Collection<Recepcion> attachedRecepcionCollectionNew = new ArrayList<Recepcion>();
            for (Recepcion recepcionCollectionNewRecepcionToAttach : recepcionCollectionNew) {
                recepcionCollectionNewRecepcionToAttach = em.getReference(recepcionCollectionNewRecepcionToAttach.getClass(), recepcionCollectionNewRecepcionToAttach.getNumeroRecepcion());
                attachedRecepcionCollectionNew.add(recepcionCollectionNewRecepcionToAttach);
            }
            recepcionCollectionNew = attachedRecepcionCollectionNew;
            transportista.setRecepcionCollection(recepcionCollectionNew);
            transportista = em.merge(transportista);
            if (tipodocumentoOld != null && !tipodocumentoOld.equals(tipodocumentoNew)) {
                tipodocumentoOld.getTransportistaCollection().remove(transportista);
                tipodocumentoOld = em.merge(tipodocumentoOld);
            }
            if (tipodocumentoNew != null && !tipodocumentoNew.equals(tipodocumentoOld)) {
                tipodocumentoNew.getTransportistaCollection().add(transportista);
                tipodocumentoNew = em.merge(tipodocumentoNew);
            }
            if (provinciaOld != null && !provinciaOld.equals(provinciaNew)) {
                provinciaOld.getTransportistaCollection().remove(transportista);
                provinciaOld = em.merge(provinciaOld);
            }
            if (provinciaNew != null && !provinciaNew.equals(provinciaOld)) {
                provinciaNew.getTransportistaCollection().add(transportista);
                provinciaNew = em.merge(provinciaNew);
            }
            if (paisOld != null && !paisOld.equals(paisNew)) {
                paisOld.getTransportistaCollection().remove(transportista);
                paisOld = em.merge(paisOld);
            }
            if (paisNew != null && !paisNew.equals(paisOld)) {
                paisNew.getTransportistaCollection().add(transportista);
                paisNew = em.merge(paisNew);
            }
            if (localidadOld != null && !localidadOld.equals(localidadNew)) {
                localidadOld.getTransportistaCollection().remove(transportista);
                localidadOld = em.merge(localidadOld);
            }
            if (localidadNew != null && !localidadNew.equals(localidadOld)) {
                localidadNew.getTransportistaCollection().add(transportista);
                localidadNew = em.merge(localidadNew);
            }
            for (Transportetransportista transportetransportistaCollectionNewTransportetransportista : transportetransportistaCollectionNew) {
                if (!transportetransportistaCollectionOld.contains(transportetransportistaCollectionNewTransportetransportista)) {
                    Transportista oldTransportistaOfTransportetransportistaCollectionNewTransportetransportista = transportetransportistaCollectionNewTransportetransportista.getTransportista();
                    transportetransportistaCollectionNewTransportetransportista.setTransportista(transportista);
                    transportetransportistaCollectionNewTransportetransportista = em.merge(transportetransportistaCollectionNewTransportetransportista);
                    if (oldTransportistaOfTransportetransportistaCollectionNewTransportetransportista != null && !oldTransportistaOfTransportetransportistaCollectionNewTransportetransportista.equals(transportista)) {
                        oldTransportistaOfTransportetransportistaCollectionNewTransportetransportista.getTransportetransportistaCollection().remove(transportetransportistaCollectionNewTransportetransportista);
                        oldTransportistaOfTransportetransportistaCollectionNewTransportetransportista = em.merge(oldTransportistaOfTransportetransportistaCollectionNewTransportetransportista);
                    }
                }
            }
            for (Recepcion recepcionCollectionNewRecepcion : recepcionCollectionNew) {
                if (!recepcionCollectionOld.contains(recepcionCollectionNewRecepcion)) {
                    Transportista oldTransportistaOfRecepcionCollectionNewRecepcion = recepcionCollectionNewRecepcion.getTransportista();
                    recepcionCollectionNewRecepcion.setTransportista(transportista);
                    recepcionCollectionNewRecepcion = em.merge(recepcionCollectionNewRecepcion);
                    if (oldTransportistaOfRecepcionCollectionNewRecepcion != null && !oldTransportistaOfRecepcionCollectionNewRecepcion.equals(transportista)) {
                        oldTransportistaOfRecepcionCollectionNewRecepcion.getRecepcionCollection().remove(recepcionCollectionNewRecepcion);
                        oldTransportistaOfRecepcionCollectionNewRecepcion = em.merge(oldTransportistaOfRecepcionCollectionNewRecepcion);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = transportista.getIdTransportista();
                if (findTransportista(id) == null) {
                    throw new NonexistentEntityException("The transportista with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Transportista transportista;
            try {
                transportista = em.getReference(Transportista.class, id);
                transportista.getIdTransportista();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The transportista with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Transportetransportista> transportetransportistaCollectionOrphanCheck = transportista.getTransportetransportistaCollection();
            for (Transportetransportista transportetransportistaCollectionOrphanCheckTransportetransportista : transportetransportistaCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Transportista (" + transportista + ") cannot be destroyed since the Transportetransportista " + transportetransportistaCollectionOrphanCheckTransportetransportista + " in its transportetransportistaCollection field has a non-nullable transportista field.");
            }
            Collection<Recepcion> recepcionCollectionOrphanCheck = transportista.getRecepcionCollection();
            for (Recepcion recepcionCollectionOrphanCheckRecepcion : recepcionCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Transportista (" + transportista + ") cannot be destroyed since the Recepcion " + recepcionCollectionOrphanCheckRecepcion + " in its recepcionCollection field has a non-nullable transportista field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Tipodocumento tipodocumento = transportista.getTipodocumento();
            if (tipodocumento != null) {
                tipodocumento.getTransportistaCollection().remove(transportista);
                tipodocumento = em.merge(tipodocumento);
            }
            Provincia provincia = transportista.getProvincia();
            if (provincia != null) {
                provincia.getTransportistaCollection().remove(transportista);
                provincia = em.merge(provincia);
            }
            Pais pais = transportista.getPais();
            if (pais != null) {
                pais.getTransportistaCollection().remove(transportista);
                pais = em.merge(pais);
            }
            Localidad localidad = transportista.getLocalidad();
            if (localidad != null) {
                localidad.getTransportistaCollection().remove(transportista);
                localidad = em.merge(localidad);
            }
            em.remove(transportista);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Transportista> findTransportistaEntities() {
        return findTransportistaEntities(true, -1, -1);
    }

    public List<Transportista> findTransportistaEntities(int maxResults, int firstResult) {
        return findTransportistaEntities(false, maxResults, firstResult);
    }

    private List<Transportista> findTransportistaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Transportista as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Transportista findTransportista(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Transportista.class, id);
        } finally {
            em.close();
        }
    }

    public int getTransportistaCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Transportista as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
     public Transportista findTransportista_x_apellido(String ape) {
       EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select u from Transportista u where u.apellidoTransportista = " + "'" + ape +"'");
            return (Transportista) q.getSingleResult();
        } finally {
            em.close();
        }
    }

    public Transportista findTransportista_x_nroDocumento(int nroDocumento) {
         EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select u from Transportista u where u.nroDocTransportista = " + "'" + nroDocumento +"'");
            return (Transportista) q.getSingleResult();
        } finally {
            em.close();
        }
    }
     public Transportista findTransportista_x_TipoDocumento(int tipodoc) {
         EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select u from Transportista u where u.idtipodoc = " + "'" + tipodoc +"'");
            return (Transportista) q.getSingleResult();
        } finally {
            em.close();
        }
    }

//    public Transportista findTransportista_x_TipoDocumentoNombre(String tipodoc) {
//        EntityManager em = getEntityManager();
//        try {
//            Query q = em.createQuery("select u from Transportista u where u.idtipodoc = " + "'" + tipodoc +"'");
//            return (Transportista) q.getSingleResult();
//        } finally {
//            em.close();
//        }
//    }



}
